package weaver.interfaces.dfhj.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022/03/08
 * @Description: 工程采购/施工合同变更审批流程
 * @ModifyLog:
 **/
/**
 *@Description: 工程采购/施工合同变更审批流程修改金额
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2022/03/08
 */
public class WeaHtbgEditJeAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        //主表
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String workflowid = requestInfo.getWorkflowid();
        writeLog( "-----WeaHtbgEditJeAction-----"+requestid );
        String htbhvalue = "";
        Map<String,String> map = new HashMap<>();
        map.put("160","cghtbh");
        map.put("175","cghtbh");
        map.put("171","nbhtbh");
        map.put("162","htbh");
        map.put("137","htbh");
        map.put("88","htxx");
        map.put("163","htxxww");
        map.put("164","htxxbl");
        Set<String> keySet = map.keySet();
        // 遍历keySet，并输出key的值
        for(String key : keySet){
            if(key.equals(workflowid)){
                htbhvalue = map.get(key).toString();
            }
        }
        writeLog( ">>>>>htbh"+htbhvalue );
        try {
           if("162".equals( workflowid )|| "163".equals( workflowid ) || "171".equals( workflowid )){
               rs.executeQuery( "select "+htbhvalue+" from " + tablename + " where requestid=?", requestid );
               rs.next();
               //合同编号
               String htbh = Util.null2String( rs.getString( htbhvalue) );
               String sql = "select htje,yfje from uf_jsfwlhttz where id =?";
               rs.executeQuery(sql,htbh);
               writeLog(sql+","+htbh);
               if (rs.next()) {
                   //合同金额
                   String htje = Util.null2String( rs.getString( "htje" ) );
                   //付款金额
                   String yfje = Util.null2String( rs.getString( "yfje" ) );
                   String wfje = floatSubtract( htje, yfje );
                   rs.executeUpdate("update uf_gccghtjbxx set wfje='"+wfje+"' where id='"+htbh+"'");
               }
           }else {
               rs.executeQuery( "select "+htbhvalue+" from " + tablename + " where requestid=?", requestid );
               rs.next();
               //合同编号
               String htbh = Util.null2String( rs.getString(htbhvalue) );
               String sql = "select htje,dpje,yfkze,wfje,wdpje from uf_gccghtjbxx where id =?";
               rs.executeQuery(sql, htbh );
               writeLog(sql+","+htbh);
               if (rs.next()) {
                   //合同金额
                   String htje = Util.null2String( rs.getString( "htje" ) );
                   //到票金额
                   String dpje = Util.null2String( rs.getString( "dpje" ) );
                   //已付款总额
                   String yfkze = Util.null2String( rs.getString( "yfkze" ) );
                   String wfje = floatSubtract( htje, yfkze );
                   String wdpje = floatSubtract( htje, dpje );
                   rs.executeUpdate("update uf_gccghtjbxx set wfje=?,wdpje=? where id=?",wfje,wdpje,htbh);
               }

           }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
    // float点型相减
    public static String floatSubtract(String a, String b) {
        a = isZero( a );
        b = isZero( b );
        BigDecimal bg1 = new BigDecimal( a );
        BigDecimal bg2 = new BigDecimal( b );
        BigDecimal bd = bg1.subtract( bg2 );
        return bd.toString();
    }

    public static String isZero(String a) {
        if ("".equals( a )) {
            a = "0";
        }
        return a;
    }
}
